package com.yk;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

public class HashSetDemo {
    public static void main(String[] args) {

        //去重
        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("登");
        list1.add("登");
        list1.add("登");
        list1.add("登");
        System.out.println(list1);

        ArrayList<String> list2 = new ArrayList<>(new HashSet<>(list1));
        System.out.println(list2);


        HashSet<String> hashSet = new HashSet<>();

        hashSet.add("纳");
        hashSet.add("居");
        hashSet.add("登");

        System.out.println(hashSet);
        System.out.println("-------------------------------------------");
        //迭代器取数据
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("-------------------------------------------");

        //增强for循环取数据
        for (String next : hashSet) {
            System.out.println(next);
        }
        System.out.println("-------------------------------------------");

        //Lambda表达式取数据
        hashSet.forEach(new Consumer<String>() {
                @Override
                public void accept(String s) {
                    System.out.println(s);
                }
        });
        System.out.println("-------------------------------------------");

        hashSet.forEach(s -> System.out.println(s));
        System.out.println("-------------------------------------------");

        hashSet.forEach(System.out::println);
        System.out.println("-------------------------------------------");

    }
}
